<?php

defined('_JEXEC') or die;

class ExercisesHelper {

    public static function addSubmenu($vName) {
        JSubMenuHelper::addEntry(
                JText::_('COM_EXERCISES_SUBMENU_EXERCISES'), 'index.php?option=com_exercises&view=exercises', $vName == 'exercises'
        );

        JSubMenuHelper::addEntry(
                JText::_('COM_EXERCISES_SUBMENU_CATEGORIES'), 'index.php?option=com_categories&extension=com_exercises', $vName == 'categories'
        );
        if ($vName == 'categories') {
            JToolBarHelper::title(
                    JText::sprintf('COM_CATEGORIES_CATEGORIES_TITLE', JText::_('com_exercises')), 'exercises-categories');
        }

        JSubMenuHelper::addEntry(
                JText::_('COM_EXERCISES_SUBMENU_QUESTIONS'), 'index.php?option=com_exercises&view=questions', $vName == 'questions'
        );

        JSubMenuHelper::addEntry(
                JText::_('COM_EXERCISES_SUBMENU_ANSWERS'), 'index.php?option=com_exercises&view=answers', $vName == 'answers'
        );
    }

    public static function getActions($categoryId = 0) {
        $user = JFactory::getUser();
        $result = new JObject;
        if (empty($categoryId)) {
            $assetName = 'com_exercises';
            $level = 'component';
        } else {
            $assetName = 'com_exercises.category.' . (int) $categoryId;
            $level = 'category';
        }

        $actions = JAccess::getActions('com_exercises', $level);
        foreach ($actions as $action) {
            $result->set($action->name, $user->authorise($action->name, $assetName));
        }

        return $result;
    }

    /**
     * @return	boolean
     * @since	1.6
     */
    public static function updateReset() {
        $user = JFactory::getUser();
        $db = JFactory::getDBO();
        $nullDate = $db->getNullDate();
        $now = JFactory::getDate();
        $query = $db->getQuery(true);
        $query->select('*');
        $query->from('#__exercises');
        $query->where("'" . $now . "' >= " . $db->quoteName('reset'));
        $query->where($db->quoteName('reset') . ' != ' . $db->quote($nullDate) . ' AND ' . $db->quoteName('reset') . '!=NULL');
        $query->where('(' . $db->quoteName('checked_out') . ' = 0 OR ' . $db->quoteName('checked_out') . ' = ' . (int) $db->Quote($user->id) . ')');
        $db->setQuery((string) $query);
        $rows = $db->loadObjectList();

        // Check for a database error.
        if ($db->getErrorNum()) {
            JError::raiseWarning(500, $db->getErrorMsg());
            return false;
        }

        JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');


        return true;
    }

    public static function getQuestionOptions() {
        // Initialize variables.
        $questid = JRequest::getVar('id');
        $options = array();

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select('id As value, exercise_name As text');
        $query->from('#__exercises AS a');
        $query->order('id');

        // Get the options.
        $db->setQuery($query);

        $options = $db->loadObjectList();

        // Check for a database error.
        if ($db->getErrorNum()) {
            JError::raiseWarning(500, $db->getErrorMsg());
        }
        $selectedlist = array();
        if ($questid) {
            $query2 = $db->getQuery(true);
            $query2->select('exer_id as exerids')->from('#__exercises_questions')->where('id=' . (int) $questid);
            $db->setQuery($query2);
            $exerids = $db->loadObject();
            $selectedlist = explode(',', $exerids->exerids);
        }


        // Merge any additional options in the XML definition.
        //$options = array_merge(parent::getOptions(), $options);
        //array_unshift($options, JHtml::_('select.option', '', JText::_('COM_EXERCISE_NO_QUESTION'),'value','text',true));
        $array = array();
        echo JHTML::_('select.genericlist', $options, 'jform[exer_id][]', 'class="inputbox" multiple="multiple" style="width:212px"', 'value', 'text', $selectedlist);
    }

    public static function getAnswerOptions() {
        // Initialize variables.
        $options = array();

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select('id As value, name As text');
        $query->from('#__exercises_questions AS a');
        $query->order('a.id');

        // Get the options.
        $db->setQuery($query);

        $options = $db->loadObjectList();

        // Check for a database error.
        if ($db->getErrorNum()) {
            JError::raiseWarning(500, $db->getErrorMsg());
        }

        // Merge any additional options in the XML definition.
        //$options = array_merge(parent::getOptions(), $options);

        array_unshift($options, JHtml::_('select.option', '0', JText::_('COM_EXERCISE_NO_ANSWER')));

        return $options;
    }

}
